using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using Utility;
using BUS;

public partial class Admin_SanPham_ThemSanPham : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDropDownList();
        }

    }

    #region"LoadDropDownList"
    public void LoadDropDownList()
    {

        tbDanhMucCollection danhMucCollection = tbDanhMucBUS.GetALL();
        ddl_DanhMuc.DataTextField = tbDanhMucColumns.TenDanhMuc.ToString();
        ddl_DanhMuc.DataValueField = tbDanhMucColumns.MaDM.ToString();
        ddl_DanhMuc.DataSource = danhMucCollection;
        ddl_DanhMuc.DataBind();
      

        tbDoanhNghiepCollection danhNghiepCollection = tbDoanhNghiepBUS.GetALL();
        ddl_DoanhNghiep.DataTextField = tbDoanhNghiepColumns.TenDN.ToString();
        ddl_DoanhNghiep.DataValueField = tbDoanhNghiepColumns.MaDN.ToString();
        ddl_DoanhNghiep.DataSource = danhNghiepCollection;
        ddl_DoanhNghiep.DataBind();
   



        tbTinhTrangSPCollection tinhTrangSPCollection = tbTinhTrangSPBUS.GetAll();
        ddl_TinhTrangSP.DataTextField = tbTinhTrangSPColumns.TenTT.ToString();
        ddl_TinhTrangSP.DataValueField = tbTinhTrangSPColumns.MaTT.ToString();
        ddl_TinhTrangSP.DataSource = tinhTrangSPCollection;
        ddl_TinhTrangSP.DataBind();


        tbNhanVienCollection nhanVienCollection = tbNhanVienBUS.GetALL();
        ddl_NhanVien.DataTextField = tbNhanVienColumns.TenNV.ToString();
        ddl_NhanVien.DataValueField = tbNhanVienColumns.MaNV.ToString();
        ddl_NhanVien.DataSource = nhanVienCollection;
        ddl_NhanVien.DataBind();

    }
    #endregion
    protected void btn_Add_Click(object sender, EventArgs e)
    {

        tbSanPham sanPham = new tbSanPham();
        sanPham.TenSP = txt_TenSP.Text;
        sanPham.DanhMuc = int.Parse(ddl_DanhMuc.SelectedValue);
        sanPham.DoanhNghiep = int.Parse(ddl_DoanhNghiep.SelectedValue);
      
        if (txt_GiaGoc.Text == "")
            txt_GiaGoc.Text = "0";
        sanPham.GiaGoc = int.Parse(txt_GiaGoc.Text);

        if (txt_GiaBan.Text == "")
            txt_GiaBan.Text = "0";
        sanPham.GiaBan = int.Parse(txt_GiaBan.Text);


        if (txt_NgayBatDau.Text == "")
        {
            sanPham.NgayBatDau = DateTime.Now;
        }
        else
        { 
            sanPham.NgayBatDau = DateTime.Parse(txt_NgayBatDau.Text);
        }


        if (txt_NgayHetHan.Text == "")
        {
            sanPham.NgayHetHan = DateTime.Now;
        }
        else
        {
  sanPham.NgayHetHan = DateTime.Parse(txt_NgayHetHan.Text);

        }
      
        if (txt_SoLuongBan.Text == "")
            txt_SoLuongBan.Text = "0";
        sanPham.SoLuongBan = int.Parse(txt_SoLuongBan.Text);

        if (txt_SoLuongToiDa.Text == "")
            txt_SoLuongToiDa.Text = "0";
        sanPham.SoLuongToiDa = int.Parse(txt_SoLuongToiDa.Text);


        sanPham.MoTa = ftb_MoTa.Text;
        sanPham.QuyDinhSuDung = ftb_QuyDinhSP.Text;

        if (txt_ChietKhau.Text == "")
        {
            txt_ChietKhau.Text = "0";
        }
        sanPham.ChietKhau = int.Parse(txt_ChietKhau.Text);

        sanPham.TinhTrangSP = int.Parse(ddl_TinhTrangSP.SelectedValue);
        sanPham.NhanVien = int.Parse(ddl_NhanVien.SelectedValue);


        if (fu_BanDo.HasFile)
        {
            string filename = DateTime.Now.Ticks.ToString() + "_" + fu_BanDo.FileName;
            fu_BanDo.SaveAs(Server.MapPath("~/Photos/BanDo/" + filename));

            System.Drawing.Image img = System.Drawing.Image.FromFile(
                Server.MapPath("~/Photos/BanDo/" + filename));

            ImageUtility.MakeThumbnailPhoto(
                img, 100, 100, Server.MapPath("~/Photos/BanDo/Thumb/"), filename);

            sanPham.BanDo = filename;

        }
        else
        {
            sanPham.BanDo = "no_images.jpg";
        }
        sanPham.DiaDiemSuDung = txt_DiaDiemSuDung.Text;

       int masp= tbSanPhamBUS.Insert(sanPham);

        if (Request.Files.Count == 1 )
        {

        }
        else
        {
            for (int i = 1; i < Request.Files.Count-1; i++)
            {
                HttpPostedFile file = Request.Files[i];
                tbHinhAnh hinh = new tbHinhAnh();
                if ((!String.IsNullOrEmpty(file.FileName)) )
                {
                    string filename = DateTime.Now.Ticks.ToString() + "_" + file.FileName;
                    file.SaveAs(Server.MapPath("~/Photos/" + filename));
                    System.Drawing.Image img = System.Drawing.Image.FromFile(
                    Server.MapPath("~/Photos/" + filename));

                    ImageUtility.MakeThumbnailPhoto(
                        img, 100, 100, Server.MapPath("~/Photos/Thumb/"), filename);

                    hinh.Hinh = sanPham.TenSP;
                    hinh.Link = filename;
                    hinh.MaSP = masp;
                    int mahinh;
                       mahinh= tbHinhAnhBUS.Insert(hinh);
                      
                     
                      

                }
            }
        }

        Response.Redirect("~/Admin/SanPham/DanhSachSanPham.aspx");

    }
}
